package encaps.utils;

import encaps.entities.OrderType;
import java.util.Iterator;
import java.util.Map;

public class StringUtil {

    private StringUtil() {
    }

    public static boolean isEmpty(String s) {
        if (s == null) {
            return true;
        }
        return s.trim().isEmpty();
    }

    public static String constructSelectQuery(String table,
            String primaryKeyColumn, Map criteria) {
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT ");
        sb.append(primaryKeyColumn);
        sb.append(" FROM ");
        sb.append(table);
        sb.append(" WHERE ");
        Iterator keyIterator = criteria.keySet().iterator();
        Object key = keyIterator.next();
        sb.append(key);
        sb.append(" LIKE '");
        sb.append(getLikePattern((String) criteria.get(key)));
        sb.append("'");

        while (keyIterator.hasNext()) {
            sb.append(" AND ");
            key = keyIterator.next();
            sb.append(key.toString());
            sb.append(" LIKE '");
            sb.append(getLikePattern((String) criteria.get(key)));
            sb.append("'");
        }
        return sb.toString();
    }

    public static String constructSelectQuery(String table,
            String primaryKeyColumn, Map criteria, int from, int count) {
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT ");
        sb.append(primaryKeyColumn);
        sb.append(" FROM ");
        sb.append(table);
        sb.append(" WHERE ");
        Iterator keyIterator = criteria.keySet().iterator();
        Object key = keyIterator.next();
        sb.append(key);
        sb.append(" LIKE '");
        sb.append(getLikePattern((String) criteria.get(key)));
        sb.append("'");

        while (keyIterator.hasNext()) {
            sb.append(" AND ");
            key = keyIterator.next();
            sb.append(key.toString());
            sb.append(" LIKE '");
            sb.append(getLikePattern((String) criteria.get(key)));
            sb.append("'");
        }
        sb.append(" LIMIT ");
        sb.append(from);
        sb.append(", ");
        sb.append(count);
        return sb.toString();
    }

    public static String getFileExtension(String fileName) {
        int i = fileName.lastIndexOf(".") + 1;
        String extension = fileName.substring(i);
        return extension;
    }

    public static String getLikePattern(String value) {
        if (isEmpty(value)) {
            return "%";
        } else {
            return "%" + value + "%";
        }
    }

    /**
     * Allows to get valid value of a string.
     *
     * @param string a string to validate.
     * @return an empty string - "" - if
     * <code>string</code> is
     * <code>null</code>, or string itself otherwise.
     */
    public static String getValidValue(String string) {
        return string == null ? "" : string;
    }

    public static boolean isDouble(String value) {
        try {
            String validValue = value.replace(',', '.');
            return true;
        } catch (NumberFormatException ex) {
            return false;
        }
    }

    public static Double parseDouble(String value) {
        String validValue = value.replace(',', '.');
        if (isEmpty(validValue)) {
            return null;
        }
        return Double.valueOf(validValue);
    }

    public static Integer parseInteger(String value) {
        if (isEmpty(value)) {
            return null;
        }
        return Integer.valueOf(value);
    }

    public static boolean isInteger(String value) {
        try {
            Integer.parseInt(value);
            return true;
        } catch (NumberFormatException ex) {
            return false;
        }
    }

    public static String constructMovieSearchQuery(String selectingCol,
            String title, String producingCountries, String genre, String personName,
            String studio, Double ratingMin, Double ratingMax, String description,
            Integer budgetMin, Integer budgetMax, Integer worldCashMin,
            Integer worldCashMax, String orderColumn, OrderType orderType,
            Integer from, Integer count) {

        StringBuilder sb = new StringBuilder();
        sb.append("SELECT ");
        sb.append(selectingCol);
        sb.append(" FROM ");
        sb.append("((movie LEFT JOIN movie_person ON movie.id = movie_person.movie_id) ");
        sb.append("LEFT JOIN person ON movie_person.person_id = person.id) ");
        sb.append("LEFT JOIN movie_genre ON movie.id = movie_genre.movie_id ");
        sb.append("WHERE title LIKE '");
        sb.append(StringUtil.getLikePattern(title));
        sb.append("' AND IFNULL(producingCountries, '') LIKE '");
        sb.append(StringUtil.getLikePattern(producingCountries));
        sb.append("' AND IFNULL(genre,'') LIKE '");
        sb.append(StringUtil.getLikePattern(genre));
        sb.append("' AND IFNULL(person.name,'') LIKE '");
        sb.append(StringUtil.getLikePattern(personName));
        sb.append("' AND IFNULL(studio,'') LIKE '");
        sb.append(StringUtil.getLikePattern(studio));
        sb.append("' ");
        if (ratingMin != null) {
            sb.append(" AND rating >= ");
            sb.append(ratingMin);
        }
        if (ratingMax != null) {
            sb.append(" AND rating <= ");
            sb.append(ratingMax);
        }
        if (budgetMin != null) {
            sb.append(" AND russianCash >= ");
            sb.append(budgetMin);
        }
        if (budgetMax != null) {
            sb.append(" AND russianCash <= ");
            sb.append(budgetMax);
        }
        if (worldCashMin != null) {
            sb.append(" AND worldCash >= ");
            sb.append(worldCashMin);
        }
        if (worldCashMax != null) {
            sb.append(" AND worldCash <= ");
            sb.append(worldCashMax);
        }
        if (!StringUtil.isEmpty(orderColumn)) {
            sb.append(" ORDER BY ");
            sb.append(orderColumn);
            if (orderType.toString() != null) {
                sb.append(" ");
                sb.append(orderType.toString());
            }
        }
        if (from != null) {
            sb.append(" LIMIT ");
            sb.append(from);
            sb.append(", ");
            sb.append(count);
        }
        String q = sb.toString();

        return q;
    }

    public static String getMultiLikePattern(String[] values) {
        StringBuilder sb = new StringBuilder();
        sb.append("%");
        for (String value : values) {
            sb.append(value.trim());
            sb.append("%");
        }
        return sb.toString();
    }
}
